package com.gin.kotlin.no_gay_alliance.interceptor

import android.os.Build
import android.util.Log
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

/**
 * 静态资源拦截器，输出最后更新时间
 */
class ResourceInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val name = request.url.pathSegments.last()

        val response = chain.proceed(request)

        response.header("Last-Modified")?.also {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val zdt = ZonedDateTime.parse(it, DateTimeFormatter.RFC_1123_DATE_TIME)
                    .withZoneSameInstant(ZoneId.systemDefault())

                Log.d(TAG, "$name, 最后更新时间: $zdt")
            }
        }


        return response
    }

    companion object {
        private val TAG = ResourceInterceptor::class.java.simpleName
    }
}